package com.lsm.searchservice.listener;

import com.alibaba.fastjson.JSONObject;
import com.hmall.common.feign.ItemServiceFeign;
import com.hmall.common.pojo.Item;
import com.lsm.searchservice.pojo.ItemDoc;
import lombok.extern.slf4j.Slf4j;
import org.elasticsearch.action.delete.DeleteRequest;
import org.elasticsearch.action.index.IndexRequest;
import org.elasticsearch.client.RequestOptions;
import org.elasticsearch.client.RestHighLevelClient;
import org.elasticsearch.common.xcontent.XContentType;
import org.springframework.amqp.core.ExchangeTypes;
import org.springframework.amqp.core.Message;
import org.springframework.amqp.rabbit.annotation.Exchange;
import org.springframework.amqp.rabbit.annotation.Queue;
import org.springframework.amqp.rabbit.annotation.QueueBinding;
import org.springframework.amqp.rabbit.annotation.RabbitListener;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Component;

import java.io.IOException;
import java.util.Map;

@Slf4j
@Component
public class SpringRabbitListener {

    @Autowired
    private RestHighLevelClient restHighLevelClient;

    @Autowired
    private ItemServiceFeign itemServiceFeign;

    @Value("${es.indexName}")
    private String indexName;

    @RabbitListener(bindings = @QueueBinding(
            value = @Queue(name = "item.remove"),
            exchange = @Exchange(name = "es.item", type = ExchangeTypes.TOPIC),
            key = "remove"
    ))
    public void removeItemListen(String id) {
        log.info("remove 消费者接收到消息.....{}", id);

        // 删除es中的数据
        DeleteRequest req = new DeleteRequest(indexName).id(id);
        try {
            restHighLevelClient.delete(req, RequestOptions.DEFAULT);
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    @RabbitListener(bindings = @QueueBinding(
            value = @Queue(name = "item.add"),
            exchange = @Exchange(name = "es.item", type = ExchangeTypes.TOPIC),
            key = "add"
    ))
    public void addItemListen(String item) {
        log.info("add 消费者接收到消息.....{}", item);


        // 添加文档
        ItemDoc doc = new ItemDoc();
        BeanUtils.copyProperties(JSONObject.parseObject(item, Item.class), doc);

        IndexRequest req = new IndexRequest(indexName).id(String.valueOf(doc.getId()));
        req.source(JSONObject.toJSONString(doc), XContentType.JSON);
        try {
            restHighLevelClient.index(req, RequestOptions.DEFAULT);
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    // 处理消息失败的消费者
    @RabbitListener(bindings = @QueueBinding(
            value = @Queue(name = "dl.q.search.error"),
            exchange = @Exchange(name = "dl.ex.search.error", type = ExchangeTypes.TOPIC),
            key = "error"
    ))
    public void errorItem(Message message) {
        Map<String, Object> headers = message.getMessageProperties().getHeaders();
        headers.entrySet().forEach(e -> {
            log.info("key:{},,,val:{}", e.getKey(), e.getValue());
        });
    }
}
